//******************************************************************************
//   MSP430F552x Demo - USCI_B0, I2C Master multiple byte TX/RX
//
//   Description: I2C master communicates to I2C slave sending and receiving
//   3 different messages of different length. I2C master will enter LPM0 mode
//   while waiting for the messages to be sent/receiving using I2C interrupt.
//   ACLK = NA, MCLK = SMCLK = DCO 16MHz.
//
//                                     /|\ /|\
//                   MSP430F5529       4.7k |
//                 -----------------    |  4.7k
//            /|\ |             P3.1|---+---|-- I2C Clock (UCB0SCL)
//             |  |                 |       |
//             ---|RST          P3.0|-------+-- I2C Data (UCB0SDA)
//                |                 |
//                |                 |
//                |                 |
//                |                 |
//                |                 |
//                |                 |
//
//   Nima Eskandari
//   Texas Instruments Inc.
//   April 2017
//   Built with CCS V7.0
//******************************************************************************

#include <driver/i2c.h>
#include <msp430.h>
#include <stdio.h>
#include "driver/board.h"
#include "driver/MLX90614.h"

char buffer[50];
void uart_transmit_0(char *buffer, uint8_t count)
{
    uint8_t i;
    for (i = 0; i < count; i++)
    {
        while (!(UCA0IFG & UCTXIFG))
            ; // USCI_A1 TX buffer ready?

        UCA0TXBUF = *(buffer + i); // TX -> RXed character
    }
}
void uart_transmit_1(char *buffer, uint8_t count)
{
    uint8_t i;
    for (i = 0; i < count; i++)
    {
        while (!(UCA1IFG & UCTXIFG))
            ; // USCI_A1 TX buffer ready?

        UCA1TXBUF = *(buffer + i); // TX -> RXed character
    }
}
int main(void)
{
    int m = 0;
    unsigned long int DATA;
    uint8_t a[5] = {0};
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

    increaseVCoreToLevel2();
    initClockTo16MHz();
    initGPIO();
    initI2C(0x5a);
    initUART_0();
    initUART_1();

    while (1)
    {
        DATA = MEM_READ(0x5a, 0x07);
        CALTEMP(DATA, a);
        snprintf(buffer, 50, "%d -- %d%d%d.%d%d \r\n", m++, a[0], a[1], a[2], a[3], a[4]);
        uart_transmit_1(buffer, strlen(buffer));
        delay_ms(700);
        P1OUT ^= BIT0;
    }
#if UART_MODE == ACLK_9600
    __bis_SR_register(LPM3_bits + GIE); // Since ACLK is source, enter LPM3, interrupts enabled
#else
    __bis_SR_register(LPM0_bits + GIE); // Since SMCLK is source, enter LPM0, interrupts enabled
#endif

    return 0;
}

//******************************************************************************
// UART RX Interrupt ***********************************************************
//******************************************************************************

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
#elif defined(__GNUC__)
void __attribute__((interrupt(USCI_A1_VECTOR))) USCI_A1_ISR(void)
#else
#error Compiler not supported!
#endif
{
    switch (__even_in_range(UCA1IV, 4))
    {
    case 0:
        break; // Vector 0 - no interrupt
    case 2:    // Vector 2 - RXIFG
        while (!(UCA1IFG & UCTXIFG))
            ;                  // USCI_A1 TX buffer ready?
        UCA1TXBUF = UCA1RXBUF; // TX -> RXed character
        break;
    case 4:
        break; // Vector 4 - TXIFG
    default:
        break;
    }
}
